package cn.yun.web.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
演示Request对象获取请求头中referer的数据
 Enumeration<>  getHeaderNames():获取请求头的所有名称 返回Enumeration对象
   String getHeader(String name):获取请求头中指定名称的数据

Enumeration:类似于迭代器
 方法: hasMoreElements():测试此枚举是否包含更多元素
          nextElement():如果此枚举对象至少有一个要提供的元素,返回此枚举的下一个元素

*/

@WebServlet("/requestDemo4")
public class RequestDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求头中 referer 数据
        String referer = request.getHeader("referer");
        System.out.println(referer);//http://localhost:8080/day14/login.html
        //防盗链
        if(referer!=null){
            if(referer.contains("/day14")){
                //正常访问
                System.out.println("播放电影........");
            }else{
                //盗链
                System.out.println("想看电影吗?来优酷吧...");
            }
        }
    }

}

